package com.xkdx.caipiao;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.xkdx.caipiao.module.network.AbsModule;
import com.xkdx.caipiao.my.MyAccountActivity;
import com.xkdx.caipiao.my.MyDetailActivity;
import com.xkdx.caipiao.presistence.historyquery.HistoryQueryAction;
import com.xkdx.caipiao.presistence.historyquery.HistoryQueryInfo;
import com.xkdx.caipiao.presistence.historyquery.HistoryQueryModule1;
import com.xkdx.caipiao.presistence.historyquery.HistoryQueryPresistence1;
import com.xkdx.caipiao.presistence.hongbao.HongbaoInfo;
import com.xkdx.caipiao.presistence.login.Login;
import com.xkdx.caipiao.presistence.login.LoginInfo;
import com.xkdx.caipiao.redpacke.BuyRedpacketList;
import com.xkdx.caipiao.redpacke.RedPackeDetail;
import com.xkdx.caipiao.setup.ShowIdentityInfo;
import com.xkdx.caipiao.statics.IConstants;
import com.xkdx.caipiao.util.Md5Utils;
import com.xkdx.caipiao.util.SharePrefenceUtil;
import com.xkdx.caipiao.util.Tost;

public class MyStrollActivity extends ThreadActivity implements OnClickListener {
	private TextView tv_name, tv_wanshan, tv_balance, tv_red_packet;
	private LinearLayout btn_success_chongzhi, btn_success_tixian, red_packe,
			ll_dingdan, ll_myaccount, ll_myred, ll_myid, ll_set;
	private static String TAG = "UserInfoFragment";
	private SharePrefenceUtil su;
	public static boolean isUpdate = false;
	private static Context context;
	View view;
	TextView title_info, red_detail, buy_lottery_tip, account;
	ViewPager viewPager;
	LinearLayout ll;
	public static LoginInfo loginInfo;

	HistoryQueryAction action;
	HistoryQueryModule1 module;
	HistoryQueryPresistence1 presistence;

	ListView lv_all, lv_no_open, lv_win;
	View view_all, view_no_open, view_win;
	List<View> viewList;
	List<HistoryQueryInfo> list, list_win, list_no_open;
	List<HongbaoInfo> list1;

	Drawable im_bottom;
	TextView tv_all, tv_win, tv_no_open, yuan;
	Button btn_recharge, btn_withdraw_cash;
	LoginInfo info;
	HongbaoInfo info1;
	float money;

	Button redpacke;
	int clickpage;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_stroll_caipiao);
		findView();
		
	}

	private void findView() {
		tv_balance = (TextView) findViewById(R.id.balance);
		tv_red_packet = (TextView)findViewById(R.id.red_packet);
		tv_name = (TextView) findViewById(R.id.login_success_name);
		tv_wanshan = (TextView) findViewById(R.id.login_success_wanshan);
		btn_success_chongzhi = (LinearLayout)findViewById(R.id.btn_success_chongzhi);
		btn_success_tixian = (LinearLayout)findViewById(R.id.btn_success_tixian);
		red_packe = (LinearLayout) findViewById(R.id.red_packe);
		ll_dingdan = (LinearLayout)findViewById(R.id.ll_dingdan);
		ll_myaccount = (LinearLayout) findViewById(R.id.ll_myaccount);
		ll_myred = (LinearLayout)findViewById(R.id.ll_myred);
		ll_myid = (LinearLayout)findViewById(R.id.ll_myid);
		ll_set = (LinearLayout) findViewById(R.id.ll_set);
		ll_dingdan.setOnClickListener(this);
		ll_myaccount.setOnClickListener(this);
		ll_myred.setOnClickListener(this);
		ll_myid.setOnClickListener(this);
		ll_set.setOnClickListener(this);
		btn_success_chongzhi.setOnClickListener(this);
		btn_success_tixian.setOnClickListener(this);
		red_packe.setOnClickListener(this);
	}

	@Override
	public void showOnPost(HashMap<String, AbsModule> result) {
		dataLoadDialog.dismiss();
		if (result.containsKey("0")) {
		
				info = getLoginInfo();
				if (info != null) {
					if (info.getVerify().equals("1")) {
						tv_wanshan.setVisibility(View.INVISIBLE);
					} else {
						tv_wanshan.setVisibility(View.VISIBLE);
					}

					if (IConstants.localUserMoney != 0) {
						DecimalFormat df = new DecimalFormat("#0.00");
						String st = df.format(IConstants.localUserMoney);
						tv_balance.setText(st);

					} else {
						if (!(TextUtils.isEmpty(info.getMoney()))) {

							float money = Float.valueOf(info.getMoney()) / 100;
							DecimalFormat df = new DecimalFormat("#0.00");
							String st = df.format(money);
							if ("0.00".equals(st)) {
								tv_balance.setText("0");
							} else {
								tv_balance.setText(st);
							}

						} else {
							tv_balance.setText("0");
						}
					}

					String nick = ("").equals(info.getUser_nickname()) ? info
							.getUser_login_name() : info.getUser_nickname();

					tv_name.setText(nick);
				}
				
				list1 = module.list1;
				if (list1.size() > 0) {
					tv_red_packet.setText(String.valueOf(list1.size()));
				}
				
			}else {
				SharePrefenceUtil su = new SharePrefenceUtil(MyStrollActivity.this,
						SharePrefenceUtil.USERINFO);
				su.clear();
				MyLotterActivity.loginInfo = null;
				IConstants.localUserMoney = 0;
				Intent intent = new Intent(MyStrollActivity.this, Login.class);
				startActivity(intent);
			}
		}
	@Override
	protected void onResume() {
		super.onResume();
		startThread();
	}

	@Override
	public void startThread() {
		if (!(dataLoadDialog.isShowing())) {
			dataLoadDialog.show();
			dataLoadDialog.setContentView(R.layout.progressbar_caipiao);
		}
		LoginInfo info = getLoginInfo();
		
		String md5 = info.getId()+ info.getLogintoken()+IConstants.key;
		String signcode = Md5Utils.md5(md5);
		action = new HistoryQueryAction(info.getId(), info.getLogintoken(), "all", "1", "1000",
				signcode);
		module = new HistoryQueryModule1(this);
		presistence = new HistoryQueryPresistence1(this);
		presistence.setActitons(action);
		presistence.setModule(module);
		presistence.execute();

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_success_chongzhi:
			Intent inten = new Intent(this, RechargeActivity.class);
			inten.putExtra("money", tv_balance.getText().toString());
			startActivity(inten);
			break;
		case R.id.btn_success_tixian:
			Intent inten1 = new Intent(this, CashRequestActivity.class);
			startActivity(inten1);
			break;
		case R.id.red_packe:
			Intent inten2 = new Intent(this, BuyRedpacketList.class);
			startActivity(inten2);
			break;
		case R.id.ll_dingdan:
			Intent intend = new Intent(this, MyDetailActivity.class);
			startActivity(intend);
			break;
		case R.id.ll_myaccount:
			Intent intenm = new Intent(this, MyAccountNewActivity.class);
			startActivity(intenm);
			break;
		case R.id.ll_myred:
			Intent intenmy = new Intent(this, RedPackeDetail.class);
			startActivity(intenmy);
			break;
		case R.id.ll_myid:
			su = new SharePrefenceUtil(this, SharePrefenceUtil.USERINFO);
			if (su.getUserInfo() != null) {
				LoginInfo user = su.getUserInfo();
				if (user != null) {
					if (user.getVerify().equals("1")) {
						Intent identity = new Intent(this,
								ShowIdentityInfo.class);
						startActivity(identity);
					} else {
						Intent intent3 = new Intent(this,
								CompleteUserInfoActivity.class);
						startActivity(intent3);
					}
				}

			} else {
				Intent intent = new Intent(this, Login.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

				startActivity(intent);
			}

			break;
		case R.id.ll_set:
			Intent intens = new Intent(this, SetupActivity.class);
			startActivity(intens);
			break;
		default:
			break;
		}
	}
	@Override
	public void onBackClick(View view) {
		finish();

	}

}
